<?php 
	include_once('../config.php'); 
	session_start();
	 
	$index = 0;
	$seasonAry = preg_split('/\+/', $_POST['season']);
	foreach ($seasonAry as $season) 
	{ 
		$index++;
		if($_POST['page'] == $index)
		{
?>


<div class="container<?php echo $index; ?>">
	<div>
      <table width="1024px" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="75%" align="left" valign="top"><p class="table-header" style="padding: 0 0 10px 40px;">Demographics<img src="../images/demographic-icon.png" width="22" height="20" alt="Demographic" class="demographic-icon"></p></td>
          <td width="25%" align="right" valign="top" class="table-link">&nbsp;</td>
        </tr></table></div>
	<div width="200px">
     <table width="1024px" border="0" cellspacing="0" cellpadding="0" style="margin: 0 10px 30px 0;">
       <tr>
         <td style="padding: 0 0 10px 0;" width="150px"><p class="demographic-title">Gender</p>
         <p>&nbsp;</p>
         <p class="demographic-body">Male</p>
         <p class="demographic-body">Female</p></td>
         <td style="padding: 0 0 10px 0;"><strong>graphic here</strong> (this table is just for style info purposes) - after codding delete the tables below</td>
       </tr>
       </table>
       
     <table width="1024px" border="0" cellspacing="0" cellpadding="0" >
<?php
	
			LinkDataBase();
			
			
			
			$Array_DBTableName = DBTableNameGet($_SESSION['ID'], $_POST['year'], $season, NULL);
			$filename = $Array_DBTableName[0]; 
			
			//Find sheet in demographics.
			$sqlDG = "select *
						FROM demographics";
			$resultDG = mysql_query($sqlDG);
			while($rowDG = mysql_fetch_assoc($resultDG)) {
				
				$table = $rowDG['table_number'];
				$title = $rowDG['title'];
				$keyword = $rowDG['keyword'];
				$contain = $rowDG['contain'];
				
				//Find question.
				$sql = "select * from ".$filename." LEFT JOIN sheet ON ".$filename.".table_name = binary(sheet.table_name) 
						WHERE table_number='".$table."' AND item NOT LIKE '%Base: All Respondents%' AND item NOT LIKE '%Base: Respondents%'";
				
				$result= mysql_query($sql);
				
				if (!$result)
					return;
				
				$numResult = mysql_num_rows($result);
				
				if ($numResult > 0) {
					
					?>
					<script type="text/javascript">
					var data = new google.visualization.DataTable();
					var myrow = new Array('');
					var Int_height = 100;
					data.addColumn('string', 'string');
						
					<?php
					$item = "";
						
					while($row = mysql_fetch_assoc($result)) {
						
						if ((($item!="")&&($row['item']!='Sigma')&&
						(strpos($row['total'],'%'))&&
						($keyword==''))||
						//For keyword exists.
						(($item!="")&&($row['item']!='Sigma')&&
						(strpos($row['total'],'%'))&&
						($keyword!='')&&
						//contain is YES.
						(((strpos($item,(string)$keyword))&&
						($contain=='YES'))||
						//contain is NO.
						((!strpos($item,(string)$keyword))&&
						($contain=='NO'))))) {
					?>
						data.addColumn('number', '<?php echo str_replace('(Net)','',$item); ?>');
                        data.addColumn({type:'string', role:'tooltip'}); //Tooltip with percentages
								
						var value = <?php echo str_replace('%','',$row['total'])/100; ?>;
						myrow.push(value, Math.round(value*100)+'%');
						Int_height = Int_height + 20;
								
					<?php
						}
						else if ($row['item']=='Sigma')
							break;
							
						$item = $row['item'];
					}
					?>
					
					// A column for custom tooltip content
                    data.addRows([myrow]);
        
					var options = {
						title: '<?php echo $title; ?>',
                        legend: {position: 'right', textStyle: {color: 'blue', fontSize: 16}},
						hAxis: {textPosition:'in', maxValue:1, minValue:0, format:'#%'},
						chartArea: {width: 700, left:0},
                        width: 1024,
						height: Int_height
                    };
					
					var chart = new google.visualization.BarChart(document.getElementById('<?php echo $index.$title; ?>_div'));
                   	chart.draw(data, options);
        
                    </script>
                
       <tr>
         <td><div id="<?php echo $index.$title; ?>_div"></div></td>
         <td>&nbsp;</td>
       </tr>
                <?php
                }
			}
?>
        <tr>
        <td colspan="2"></tr>
      </table>
      </div>
</div>

<?php
}
}
?>